package com.sz.app.customer.api.trade;

import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.constants.MessagesCodes;
import com.sz.biz.common.customer.entity.CusUser;
import com.sz.biz.trade.ord.entity.TrdOrderType;
import com.sz.biz.common.customer.service.CustomerUserService;
import com.sz.biz.trade.goods.service.GoodsService;
import com.sz.biz.trade.goods.dto.GoodsDto;
import com.sz.biz.trade.goods.dto.GoodsInfoDto;
import com.sz.biz.trade.goods.dto.GoodsInfoShortDto;
import com.sz.biz.trade.goods.dto.GoodsListCustomerDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.core.service.PrincipalUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Function: 商品管理 <br>
 * Author: jifeixing <br>
 * Date: 2017-07-19 14:18:00
 */
@RestController
@RequestMapping("${api}/cust/trd/goods")
@Api(description = " ", tags = "044、贸易-商品管理")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CustomerUserService customerUserService;

    /**
     * 新建商品
     *
     * @throws Exception
     */
    @ApiOperation(value = "新建商品", notes = "新建商品")
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public ResultDto save(@RequestBody @ApiParam(value = "商品信息", required = true) @Validated GoodsDto goodsDto) throws ParseException {
        Integer id = goodsService.save(goodsDto);
        ResultDto result = this.getSaveSuccessDto(MessagesCodes.MODULE_TRADE_GOODS);
        result.setData(id);
        return result;
    }

    /**
     * 编辑商品
     *
     * @throws Exception
     */
    @ApiOperation(value = "编辑商品", notes = "编辑商品")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public ResultDto update(@RequestBody @ApiParam(value = "商品信息", required = true) @Validated GoodsDto goodsDto) throws ParseException {
        goodsService.update(goodsDto);
        return this.getUpdateSuccessDto(MessagesCodes.MODULE_TRADE_GOODS);
    }

    /**
     * 删除商品
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除商品", notes = "删除商品")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "ids", value = "商品id数组： [1,2]", required = true, dataType = "array", paramType = "body")})
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public ResultDto batchDeleteByIds(@RequestBody List<Integer> ids) {
        goodsService.batchDeleteByIds(ids);
        return getDeleteSuccessDto(MessagesCodes.MODULE_TRADE_GOODS);
    }

    /**
     * 商品启用,停用
     * @param map
     * @return
     */
    @ApiOperation(value = "商品启用,停用", notes = "商品启用")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "map", value = "ids(商品id数组： [1,2]), \r\nisEnabled(true: 启用；false： 停用)", required = true, dataType = "Map", paramType = "body")})
    @RequestMapping(value = "/enabled", method = {RequestMethod.POST})
    public ResultDto enabledByIds(@RequestBody Map<String, Object> map) {
        Boolean isEnabled = goodsService.enableByIds(map);
        if(isEnabled){
            return getOnlineSuccessDto();
        }else{
            return getOfflineSuccessDto();
        }
    }

    /**
     * 根据id查询商品
     */
    @ApiOperation(value = "根据id查询商品", notes = "根据id查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id ", required = true, dataType = "Integer", paramType = "path")})
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getGoodsById(@PathVariable Integer id) {
        GoodsInfoDto goodsDto = goodsService.findById(id);
        ResultDto dto = new ResultDto();
        dto.setData(goodsDto);
        return dto;
    }

    /**
     * 商品列表
     */
    @ApiOperation(value = "商品列表", notes = "商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "supplierId", value = "供应商id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "goodsTypeId", value = "商品类型(多个类型时候以，区分)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto findGoodsList(@RequestParam(required = false) String q,
                                   @RequestParam(required = false) Integer supplierId,
                                   @RequestParam(required = false) String goodsTypeId,
                                   @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                   @RequestParam(required = false, defaultValue = "10") int pageSize,
                                   @RequestParam(required = false) String sort,
                                   @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        Integer purchaserId = PrincipalUtils.getAccountId();
        CusUser user = customerUserService.findById(purchaserId);
        return goodsService.findByFilterCustomer(q, 2, TrdOrderType.SALE, goodsTypeId, null, supplierId, user.getRefCustomerId(), pageIndex, pageSize, sort, asc);
    }

    /**
     * 商品列表
     */
    @ApiOperation(value = "通过商品ids获取商品列表", notes = "通过商品ids获取商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ids数组", dataType = "string", paramType = "query")})
    @RequestMapping(value = "/getbyids", method = {RequestMethod.GET})
    public ResultDto findGoodsListByIds(@RequestParam(required = false) String ids) {
        List<GoodsListCustomerDto> result = goodsService.findGoodsListByIds(ids);
        ResultDto dto = new ResultDto();
        dto.setData(result);
        return dto;
    }

    /**
     * 客户订单列表商品初始化
     */
    @ApiOperation(value = "客户订单列表商品初始化,返回id,code,name", notes = "客户订单列表商品初始化,返回id,code,name")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "orderType", required = true, value = "订单类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sellId", value = "销售方id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "purchaserId", value = "采购商id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/short", method = {RequestMethod.GET})
    public ResultDto findGoodsShort(@RequestParam(required = false) String q,
                                        @RequestParam(required = false, defaultValue = "2") Integer businessType,
                                        @RequestParam(required = true) TrdOrderType orderType,
                                        @RequestParam(required = false) Integer sellId,
                                        @RequestParam(required = false) Integer purchaserId,
                                        @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                        @RequestParam(required = false, defaultValue = "10") int pageSize,
                                        @RequestParam(required = false) String sort,
                                        @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        return goodsService.findGoodsShortListCustomer(q, businessType, orderType, sellId, purchaserId, pageIndex, pageSize, sort, asc);
    }

    /**
     * 商品列表,返回订单需要字段
     */
    @ApiOperation(value = "商品列表,返回订单需要字段", notes = "商品列表,返回订单需要字段")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", dataType = "int", paramType = "path")})
    @RequestMapping(value = "/{id}/order", method = {RequestMethod.GET})
    public ResultDto findGoodsShortListForOrder(@PathVariable Integer goodsId) {
        GoodsInfoShortDto goodsDto = goodsService.findShortById(goodsId);
        ResultDto dto = new ResultDto();
        dto.setData(goodsDto);
        return dto;
    }
}
